package com.hadoop.demo.mapreduce.lottery;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * =================================
 * <p>
 * Created by cjj on 2018/10/16.
 * <p>
 * 描述:
 */

public class CountNumReduce extends Reducer<Text,LongWritable,Text,LongWritable> {

    /**
     * 框架在所有map处理完成之后 将所有key-value缓存起来，进行分组，然后传递一个组<key,values[]>,再调用此方法
     * @param key
     * @param values
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
        long count = 0;

        for(LongWritable value : values){
            count+=value.get();
        }
        context.write(key,new LongWritable(count));
    }
}